% /*******************************************************************************
%  * Main Simulation Routine
%  * *****************************************************************************
%  * @param lower
%  * @param upper
%  * @param m
%  * @param n
%  * @param r
%  * @param k
%  * @param q
%  * @param t
%  * @param z
%  */
function test_err(lower,upper,m,n,r,k,q,t,z,fid)

step_a = 2^(-m);
step_b = 2^(-n);
num = (round((upper-lower)/step_a) + 1)*(round((upper-lower)/step_b) + 1);
if(~t)
    fprintf(fid,'a');
    fprintf(fid,',');
    fprintf(fid,'b');
    fprintf(fid,',');
    fprintf(fid,'ans_true');
    fprintf(fid,',');
    fprintf(fid,'ans_true_r');
    fprintf(fid,',');
    fprintf(fid,'ans_const');
    fprintf(fid,',');
    fprintf(fid,'ans_eric');
    fprintf(fid,',');
    fprintf(fid,'ans_hyb');
    fprintf(fid, ',');
    fprintf(fid, 'corr_const');
    fprintf(fid, ',');
    fprintf(fid, 'corr_eric');
    fprintf(fid, ',');
    fprintf(fid, 'corr_hyb');
    fprintf(fid, '\n');       
end
index = 0;
a_max = 0.0;
b_max = 0.0;
true_max = 0.0;
max_abs_err = 0.0;
max_ans = 0.0;
ans_true_r = 0.0;
sqr_err = 0.0;
avg_err = 0.0;
for a = lower:step_a:upper
    for b = lower:step_b:upper
        index = index + 1
        ans_true = a * b;
        ans_trunc = trunc_mult(a,b,0.0,m,n,r,k);
        if ((z == 1) || (z == 0) || (z == 4))
                [ans_const, corr_const] = ccm(a, b, m, n, r, k);
        end
        if ((z == 2) || (z == 0) || (z == 4))
                [ans_eric, corr_eric] = vcm(a, b, m, n, r, k);
        end
        if ((z == 3) || (z == 0))
                [ans_hyb, corr_hyb] = hcm(a, b, m, n, r, k, q);
        end
        
        err = ans_true - ans_trunc;
        abs_err = abs(err);
        sqr_err = sqr_err + err * err;
        avg_err = avg_err + err;
        if (abs_err > max_abs_err)
            a_max = a;
            b_max = b;
            true_max = ans_true;
            max_abs_err = abs_err;
            max_ans = ans_trunc;
        end
        
        ans_true_r = rnd_near(ans_true, r);
        
        if(~t)
            
            disp_bin(a,0,m,fid);
            fprintf(fid, ',');
            disp_bin(b, 0, n, fid);
            fprintf(fid, ',');
            disp_bin(ans_true, 0, m + n, fid);
            fprintf(fid, ',');
            disp_bin(ans_true_r, 0, r, fid);
            fprintf(fid, ',');
            disp_bin(ans_const, 0, r, fid);
            fprintf(fid, ',');
            disp_bin(ans_eric, 0, r, fid);
            fprintf(fid, ',');
            disp_bin(ans_hyb, 0, r, fid);
            fprintf(fid, ',');
            fprintf(fid, '%1.25g', corr_const);
            fprintf(fid, ',');
            fprintf(fid, '%1.25g', corr_eric);
            fprintf(fid, ',');
            fprintf(fid, '%1.25g', corr_hyb);
            fprintf(fid, '\n');
        end
        
        
    end
end
end